<%--
  Created by IntelliJ IDEA.
  User: lanhai
  Date: 2021/4/10
  Time: 12:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<body>
<%@include file="include-head.jsp" %>
<%--引入pagination的css--%>
<link href="css/pagination.css" rel="stylesheet" />
<link rel="stylesheet" href="ztree/zTreeStyle.css"/>

<%--引入基于jquery的paginationjs--%>
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<%@include file="include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input class="form-control has-success" id="inputKeyword" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="searchBtn" type="button" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button type="button" id="batchRemoveBtn" class="btn btn-danger" style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button type="button" class="btn btn-primary"
                            style="float:right;" id="showAddModalBtn">
                        <i class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <%--  tbody的id=rolePageTBody,用于绑定on()函数 --%>
                            <tbody id="rolePageTBody">
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<%@include file="/WEB-INF/modal-role-add.jsp"%>
<%@include file="/WEB-INF/modal-role-edit.jsp"%>
<%@include file="/WEB-INF/modal-role-confirm.jsp"%>
<%@include file="/WEB-INF/modal-role-assign-auth.jsp"%>
<script type="text/javascript" src="crowd/my-role.js"></script>
<script>



    $(function () {


        window.pageNum = 1;
        window.pageSize = 5;
        window.keyword = "";


        // 调用页面初始化函数
        generatePage();

        // 搜索关键词
        $("#searchBtn").click(function () {
            // 1.获取关键词
            window.keyword = $("#inputKeyword").val();
            window.pageNum = 1;
            // 2.调用分页函数
            generatePage();
        });

        // 点击‘新增’按钮打开模态窗
        $("#showAddModalBtn").click(function () {
            $("#addRoleModal").modal("show");
        });



        // 角色添加函数
        roleAddMet();

        // 角色修改函数
        roleEditMet();

        // 角色删除函数
        roleRemoveMet();

        // 全选函数
        roleCheckAll();

        // 分配权限按钮绑定click
        roleAddAuthMet();

        // 分配给权限模态框中的“分配”按钮绑定click
        authAddConfirmMet();

    });

    function authAddConfirmMet() {
        $("#assignAuthBtn").click(function () {
            // 1.获取被勾选的authId
            var authIdArray = [];

            // 2.获取zTreeObj对象
            var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");

            // 3.获取全部被勾选节点
            var checkedNodes = zTreeObj.getCheckedNodes();

            // 4.遍历checkedNodes
            for (var i = 0; i < checkedNodes.length; i++) {
                var node = checkedNodes[i];

                var authId = node.id;

                authIdArray.push(authId);
            }

            // 5.发送ajax请求
            var requestBody = {
                "authIdArray" : authIdArray,

                // 为了服务端handler方法方便接收，所以roleId也写成数组的形式
                // @RequestBody Map<String, List<Integer>>
                "roleId" : [window.roleId]
            };

            requestBody = JSON.stringify(requestBody);

            $.ajax({
                "url" : "assign/do/role/assign.json",
                "type" : "post",
                "data" : requestBody,
                "contentType" : "application/json;charset=UTF-8",
                "dataType" : "json",
                "success" : function (res) {
                    if(res.state == "SUCCESS") {
                        layer.msg("保存成功！");

                        // 重新加载分页数据
                        generatePage();

                    }else if(res.state == "ERROR"){
                        layer.msg("操作失败！" + res.message);
                    }
                },
                "error" : function (res) {
                    layer.msg(res,status + " " + res.statusText);
                }
            });

            // 隐藏模态框
            $("#assignAuthModal").modal("hide");
        });
    }

    // 分配权限按钮绑定click
    function roleAddAuthMet() {
        $("#rolePageTBody").on("click", ".checkBtn", function () {
            $("#assignAuthModal").modal("show");

            // 将roleId放进全局变量中
            window.roleId = this.id;

            // 获取权限树形数据
            fillAuthTree();
        });

    }

    function roleEditMet() {
        // on()绑定click，打开修改模态框
        $("#rolePageTBody").on("click", ".pencilBtn", function () {
            $("#editRoleModal").modal("show");

            // 获取roleName和roleId
            var roleName = $(this).parent().prev().text();
            window.roleId = this.id;
            $("#roleNameEditInput").val(roleName);
        });

        // 给模态框保存绑定点击事件
        $("#editRoleBtn").click(function () {
            var editRoleName = $("#roleNameEditInput").val();
            $.ajax({
                "url" : "role/edit.json",
                "type" : "post",
                "data" : {
                    "name" : editRoleName,
                    "id" : window.roleId
                },
                "dataType" : "json",
                "success" : function (res) {
                    if(res.state == "SUCCESS") {
                        layer.msg("保存成功！");

                        // 重新加载分页数据
                        generatePage();

                    }else if(res.state == "ERROR"){
                        layer.msg("操作失败！" + res.message);
                    }
                },
                "error" : function (res) {
                    layer.msg(res,status + " " + res.statusText);
                }
            });

            // 关闭模态框
            $("#editRoleModal").modal("hide");
            $("#roleNameEditInput").val("");
        });

    }
    function roleRemoveMet() {

        // 调用弹出模态框函数
        $("#rolePageTBody").on("click", ".removeBtn", function () {

            var roleName = $(this).parent().prev().text();

            // 准备roleArray
            var roleArray = [{
               "roleId" : this.id,
               "roleName" : roleName
            }];

            showConfirmRoleModal(roleArray);
        });



        // 点击确认删除按钮触发事件
        $("#removeRoleBtn").click(function () {

            // 后台用RequestBody接收的数据用JSON.stringify()处理后在发送
            var requestBody = JSON.stringify(window.roleIDArray);

            $.ajax({
                "url" : "role/remove/by/role/id/remove.json",
                "type" : "post",
                "data" : requestBody,
                "contentType" : "application/json;charset=UTF-8",
                "dataType" : "json",
                "success" : function (res) {
                    if(res.state == "SUCCESS") {
                        layer.msg("删除成功！");

                        // 重新加载分页数据
                        generatePage();

                    }else if(res.state == "ERROR"){
                        layer.msg("操作失败！" + res.message);
                    }
                },
                "error" : function (res) {
                    layer.msg(res,status + " " + res.statusText);
                }
            });

            // 关闭模态框
            $("#confirmModal").modal("hide");
        });

        // 点击批量删除按钮
        $("#batchRemoveBtn").click(function () {

            // 准备一个roleArray
            var roleArray = [];

            $(".roleCheckbox:checked").each(function () {
                var roleId = this.id;
                var roleName = $(this).parent().next().text();

                roleArray.push({
                    "roleId" : roleId,
                    "roleName" : roleName
                });
            });

            if(roleArray.length == 0) {
                layer.msg("请至少选中一个再删除！");
                return;
            }

            // 调用弹窗进行删除确认
            showConfirmRoleModal(roleArray);
        });
    }
    function roleAddMet() {
        // 保存按钮绑定响应函数
        $("#saveRoleBtn").click(function () {
            // 1.获取Input中的值
            var roleName = $("#addRoleModal [name=roleName]").val().trim();

            // 2.发送AJAX请求
            $.ajax({
                "url" : "role/save.json",
                "type" : "post",
                "data" : {"name" : roleName},
                "dataType" : "json",
                "success" : function (res) {
                    if(res.state == "SUCCESS") {
                        layer.msg("保存成功！");

                        // 重新加载分页数据
                        window.pageNum = 9999999;
                        generatePage();
                    }else if(res.state == "ERROR"){
                        layer.msg("操作失败！" + res.message);
                    }
                },
                "error" : function (res) {
                    layer.msg(res,status + " " + res.statusText);
                }
            });

            // 关闭模态框
            $("#addRoleModal").modal("hide");
            $("#addRoleModal [name=roleName]").val("");

        });
    }




    function roleCheckAll() {
        $("#summaryBox").click(function () {
            // 获取当前全选框的选中状态
            var currentStatus = this.checked;

            // 用全选框的状态写入到每一个单选框中
            $(".roleCheckbox").prop("checked", currentStatus);
        });

        // 单选框全选和全不选
        $("#rolePageTBody").on("click", ".roleCheckbox", function () {
            // 先获取全部单选框已选取数量
            var checkedBoxCount = $(".roleCheckbox:checked").length;

            // 获取全部单选框的数量
            var checkBoxCount = $(".roleCheckbox").length;

            // 相等为true 否则为false
            $("#summaryBox").prop("checked", checkedBoxCount == checkBoxCount);





        });
    }


</script>

</body>
</html>

